package cn.edu.qfnu.oj.web.controller;

import cn.edu.qfnu.common.utils.NumberUtils;
import cn.edu.qfnu.oj.entity.*;
import cn.edu.qfnu.oj.service.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "query/")
public class QueryController {

    private final StudentService studentService;
    private final TeacherService teacherService;
    private final ContestService contestService;
    private final TaskService taskService;
    private final ProblemService problemService;

    @Autowired
    public QueryController(StudentService studentService, TeacherService teacherService,
                           ContestService contestService, TaskService taskService,
                           ProblemService problemService) {

        this.studentService = studentService;
        this.teacherService = teacherService;
        this.contestService = contestService;
        this.taskService = taskService;
        this.problemService = problemService;
    }

    //学生列表
    @RequestMapping(value = "students", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> studentList() {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            List<Student> students = studentService.query();
            jsonMap.put("result", "success");
            jsonMap.put("data", students);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }

    //老师列表

    @RequestMapping(value = "teachers", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> teacherList() {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            List<Teacher> teachers = teacherService.query();
            jsonMap.put("result", "success");
            jsonMap.put("data", teachers);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }
    //比赛列表

    @RequestMapping(value = "contests", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> contestList() {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            List<Contest> contests = contestService.query();
            jsonMap.put("result", "success");
            jsonMap.put("data", contests);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }
    //作业列表

    @RequestMapping(value = "tasks", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> taskList() {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            List<Task> tasks = taskService.query();
            jsonMap.put("result", "success");
            jsonMap.put("data", tasks);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }

    //问题列表
    @RequestMapping(value = "problems", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> problemList() {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            List<Problem> problems = problemService.query();
            jsonMap.put("result", "success");
            jsonMap.put("data", problems);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }

    @RequestMapping(value = "problemByKeyWords", method = RequestMethod.POST)
    public Map<String, Object> problemsByKeywords(String keyWords) {

        Map<String, Object> jsonMap = new HashMap<>();
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Problem.class);
        try {
            detachedCriteria
                    .add(Restrictions.or(Restrictions.like("title", keyWords, MatchMode.START)))
                    .add(Restrictions.or(Restrictions.like("content", keyWords, MatchMode.START)));
            if (NumberUtils.canParseInteger(keyWords)) {
                detachedCriteria.add(Restrictions.or(Restrictions.eq("id", keyWords)));
            }
            List<Problem> problems = problemService.queryByDetachedCriteria(detachedCriteria);
            jsonMap.put("result", "success");
            jsonMap.put("data", problems);
        } catch (Exception e) {
            jsonMap.put("result", "error");
        }
        return jsonMap;
    }
}
